package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.Teacher;
import com.example.kaogong.sys.mapper.TeacherMapper;
import com.example.kaogong.sys.pojo.vo.TeacherInfoVo;
import com.example.kaogong.sys.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 * 教师 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页获取所有教师信息
     * @param current 当前页
     * @param size 每页大小
     * @return
     */
    @Override
    public RespBean<Page<TeacherInfoVo>> getAllTeacherInfo(Long current, Long size) {
        try {
            Page<TeacherInfoVo> teacherInfoVoPage = teacherMapper.getAllTeacherInfo(new Page<>(current, size));
            return RespBean.success(teacherInfoVoPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }

    /**
     * 根据教师编号删除一条教师信息
     * @param tid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespBean deleteTeacherByTid(Integer tid) {
        try {
            // 查询教师信息
            Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("teacher_id", tid));
            if (teacher == null)
                return RespBean.error("删除失败， 该教师不存在");

            // 逻辑删除教师信息
            teacher.setDeleted(true);
            int update = teacherMapper.update(teacher, new UpdateWrapper<Teacher>().eq("teacher_id", tid));
            if (update < 1)
                return RespBean.error("删除教师信息失败");

            // 逻辑删除教师对应用户
            Integer delete = userMapper.deleteUserByUid(teacher.getUid());

            // 删除失败 回滚
            if (delete < 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return RespBean.error("删除教师对应用户信息失败");
            }

        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespBean.error("删除教师信息失败");
        }
        return RespBean.success("删除教师信息成功");
    }

    /**
     * 修改教师信息
     * @param teacher 教师对象
     * @return
     */
    @Override
    public RespBean updateTeacherInfo(Teacher teacher) {
        try {
            teacher.setUid(null);
            teacher.setDeleted(null);
            int update = teacherMapper.update(teacher, new UpdateWrapper<Teacher>().eq("teacher_id", teacher.getTeacherId()));
            if (update >= 1)
                return RespBean.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }
}
